﻿using AutoMapper;
using MediatR;
using Medical.Api.Write.Application.Command.PackagingSterilization.SterilizationRegistration;
using Medical.Domain.PackagingSterilization;
using Medical.ErrorCode;
using Medical.Interstructrue.Base;

namespace Medical.Api.Write.Application.Handler.PackagingSterilization.Sterilization
{
    /// <summary>
    /// 添加灭菌登记
    /// </summary>
    public class AddSterilizationRegistrationHandler : IRequestHandler<AddSterilizationRegistrationCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<SterilizationRegistration> sterilizationRepository;
        private readonly IMapper mapper;
        private readonly ILogger<AddSterilizationRegistrationHandler> logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sterilizationRepository"></param>
        /// <param name="mapper"></param>
        /// <param name="logger"></param>
        public AddSterilizationRegistrationHandler(IBaseRepository<SterilizationRegistration> sterilizationRepository, IMapper mapper, ILogger<AddSterilizationRegistrationHandler> logger)
        {
            this.sterilizationRepository = sterilizationRepository;
            this.mapper = mapper;
            this.logger = logger;
        }

        /// <summary>
        /// 添加灭菌登记实体
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<int>> Handle(AddSterilizationRegistrationCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            int successCount = 0;

            // 循环添加每个包的灭菌登记记录
            foreach (var packageId in request.EquipmentPackageIds)
            {
                var model = new SterilizationRegistration
                {
                    BatchNumber = request.BatchNumber,
                    SterilizationMode = request.SterilizationMode,
                    EquipmentId = request.EquipmentId,
                    SterilizationBatch = request.SterilizationBatch,
                    Operator = request.Operator,
                    StartTime = request.StartTime,
                    SterilizationDuration = request.SterilizationDuration,
                    Status = request.Status,
                    CompletionOperator = request.CompletionOperator,
                    CompletionTime = request.CompletionTime,
                    SterilizationResult = request.SterilizationResult,
                    EquipmentPackageIds = packageId
                };

                var result = sterilizationRepository.Create(model);
                if (result > 0)
                {
                    successCount++;
                }
            }

            if (successCount > 0)
            {
                res.Data = successCount;
                res.Code = ApiEnums.Success;
                res.Msg = $"成功添加{successCount}条灭菌登记记录";
                logger.LogInformation($"成功添加{successCount}条灭菌登记记录");
                return Task.FromResult(res);
            }
            else
            {
                res.Data = 0;
                res.Code = ApiEnums.Fail;
                res.Msg = "添加灭菌登记实体失败";
                logger.LogError("添加灭菌登记实体失败");
                return Task.FromResult(res);
            }
        }
    }
}
